package com.lzy.mall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.lzy.mall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * @author lzy
 * @date 2021/10/13 9:52
 */
@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private  String app_id = "2021000118631352";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDIevCKApePvuDO3ci0x31IlGAYWdSvHFt6BVpnY8Qp0dxLqrTt/1Auc3BeGwnpfzT2LDGQZC/Xd4H2M1TWTQd1yEgfImVv2PVbeNEftZd0perJn5VDTdJhXw5fqODib32bSOOKzTBDE4KY6cWD4nqM1F7+XHn/1VXdh6NkRBYwMaXtwz0G4JaFBCZocdKN+9PgOnthaQxF1kiJ3Yr7HKiBdzOEBdTGCBj/8oxKclO/DquZXvp2dv6baT7tL2aCYoMbhSLxgLkF64LUJ/4HlG3u8MVp6YR9d2wbGnIKC/pKhNqmItn4XYy46jgNXuSH/JBhE4vdj696fyse87cqgs1LAgMBAAECggEBAJlBOe4Wiyvsu8gHzO09bq10F32l9isi318JBpHwtz1M2z3Qsl0Zhn7r6mNWAh7ICoGBSSZbICNzjRrbJKhp3CESyOdcEPUOgom5cwvtcmlOiIw2/RClRgT/eGpyG++dUQTLFH6/2nv8RyKD2mS2Vf6gPjnuEakbuXcOwD3euqN9g60SDIXkamhIuOq0HIcBp3hGuLO8S1mcofzxgQ2Zwj4KMBYeXLIQpxxu03iPYTONnRmnqNL8HKo/gEqhJumm+Lg+BD8v3l7EILpRa5mTCL9KUMdbJdBVVSvHJ2uOtrjnmv7QVfVdCtemRrPAIH/qjOzGmk+gcsHxIyqDzjo3IZECgYEA/+YkGafasOvJKVXK3tlxntkBM2J1cMyiCvR5CkHZQiMahs9t3PNV6hWQGQI7i8ma7VTUXPzFAzCvvWGv+lRS+xCtKUblMsfdBVYUWKkw8nHm6QzmxwSXMLW9ZIwHkTPJVOo7cJiszBpZjPYNEE8R7vN+QHganp5ivMghehqwIQMCgYEAyI8yzO7GSZsIlrmPq5A1KoHgErFAME3rGDo1lsmhlgY+uEhqRXXc7n/8ptgMN4x7EpRVYMBYRQnKEHzz55mUC0nhaSSAz+smvTsbGgmeFgJ2W8uVdZV5pLhkbiw2Yju4pWM4YkxS8GSstaXhkZbYm9yaV7cO6DlaA8eJAUf73BkCgYEA7QcfKSRxTK8aBU+SOhjvRVN76Typy3HAJqWL+BRGSRx61aQM/zvluI2Ai6mlvjVfa7XHftojInFWVvTFK/ovYp5vxmwEoKS/9NFmxzjwPaGDYRqyKGX4JQYyB5As8jIlqqHLrN++Ls+5YvgF4bVbxxmDN/QHtCTBEFvPpmpR+kECgYEAkwRtn84UqpYsCJkD2Sa/4mYMBIfMXtxvTnY1HFC304rGZT4oMLzCdE57vS44XmvrN+ZmY6L70bfoWIfQH2D0JgZFZ1ar2j4tSp/OMRsFuGqbqpSk/enRns7QR/a+8umGZC5EgcIjNjcGZ62umZOzOyXOOihllBOeiZC2PKsw09kCgYB3hSmDA1WNjk2YHgjQpNcJ4sQnNaxFLhWdMgfOLyJmXKVWCwb49RyuN9nsEzBz/H7g8li4IB6LkFQGPzhrXXwwKqlnWD+OeGRoBCgMHAuuMjncJHSo+U0+XNwN1/80gVReEptzuA4Ili3vWX5ZBQxTsr2vrHTLV5VN+a+bQr4uXQ==";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAmBjNQsa6TmQrpbAPbr3a/Gt7Cn3tFpTEatIcu0IzVAUrmbFlQ9cQfi/3ffPTr/TAhmUbAU9Uv1WkUIqrCimvU923sqHpTrO3gN8fuOMdrZNk0YPGpZmTC2PjJ3TcK01ID3yikjJ3Mr8Ka6Hbh7HQo+8HivI1zW+k/63EVXvQFCnZHCWYZeCEXZ3r3d0+WdFYH27C2rVfIlOJgrwPtkvvf0QC1pGFiL5nSxqg8vIk4lqR/q9+GIei0D90lTLE8vBtmuxSd/vJn2cFCQ4H5RoqZEaanjUwZKZEtt7dbeASrkzhVqK5htoccGpkcIiJ2Y9ymHj2eu6sPdom6tuJ3yQF9QIDAQAB";

    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url = "http://139.9.66.198:9003/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://member.lzyan.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 自动关单时间
    private String timeout = "15m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        // 30分钟内不付款就会自动关单
        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\"" + timeout + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        return result;
    }
}